<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Kamar extends Authenticated{
	
	function __construct() {
		parent::__construct();
		$this->load->model("kamar_model");
                $this->load->model("fungsi_model");
                $this->load->model("kategori_kamar_model");
                $this->load->model("data_customer_model");
                $this->load->model("servis_model");
	}
	
	function index(){
		$this->list_kamar() ;
	}
	
        function list_kamar()
        {
            $data['list_kamar']=$this->kamar_model->get_all_kamar();
            $this->load->view('header');
            $this->load->view('kamar/list_kamar',$data);
            $this->load->view('footer');
        }
        
        function get_kamar()
        {
            $data_temp=  $_GET['kamar'];
            //echo $data_temp;
            $temp = explode("\"",$data_temp);
            $data['id'] = $temp[3];
            $result = $this->kamar_model->get_kamar_join_kategori($data['id']);
            $data_customer['kamar_id'] = $data['id'];
            $data_customer['date_pesan_masuk <='] = date("2012-".$_GET['bulan']."-".$_GET['tanggal']);
            $data_customer['date_pesan_keluar >='] = date("2012-".$_GET['bulan']."-".$_GET['tanggal']);
            $data_customer['status_pesan'] = "pesan";
            $filter_result2 = $this->data_customer_model->get_customer_clause($data_customer);
//            die($this->db->last_query());
            $result['cust_id'] = 0;
            if($filter_result2)
            {
                $result['cust_id'] = $filter_result2['id'];
                if($filter_result2['waktu_checkout']!="0000-00-00 00:00:00")
                    $result['status'] = "checked out";
                else if($filter_result2['waktu_checkin']!="0000-00-00 00:00:00")
                {
                    $result['status'] = "checked in";
                    $data_trax['cust_id'] = $result['cust_id'];
                    if($filter_result2['adjoin_to']>0)
                        $data_trax = "cust_id = ".$result['cust_id']." OR cust_id = ".$filter_result2['adjoin_to'];
                    $result['trax_list'] =  $this->servis_model->get_trax_cust_clause($data_trax);
                }
                else
                {
                    $result['status'] = "pesan";
                    $data_customer = array();
                    $data_customer['waiting_to'] = $result['cust_id'];
                    $result['waiting_list'] =  $this->data_customer_model->get_customer_join_data_clause($data_customer);
                    $data_customer = array();
                    $data_customer['customer.id'] = $result['cust_id'];
                    $result['data_customer'] =  $this->data_customer_model->get_customer_join_data_clause($data_customer);
                }
            }
            else
            {
                $result['status'] = "free";
            }
            echo json_encode($result);
        }
        
        function get_all_kamar()
        {
            //echo $data_temp;
            $filter_result = $this->kamar_model->get_all_kamar_join_kategori();
            
            echo json_encode($filter_result);
        }
        
        function get_status_kamar()
        {
            
            $data_customer['kamar_id'] = $_GET['kamar'];
            $data_customer['date_pesan_masuk <='] = date("2012-".$_GET['bulan']."-".$_GET['tanggal']);
            $data_customer['date_pesan_keluar >='] = date("2012-".$_GET['bulan']."-".$_GET['tanggal']);
            $data_customer['status_pesan'] = "pesan";
            $filter_result = $this->data_customer_model->get_customer_clause($data_customer);
            $result['id'] = $_GET['id'];
            $result['tanggal'] = $_GET['tanggal'];
            if($filter_result)
            {
                if($filter_result['waktu_checkout']!="0000-00-00 00:00:00")
                    $result['status'] = "checked out";
                else if($filter_result['waktu_checkin']!="0000-00-00 00:00:00")
                    $result['status'] = "checked in";
                else
                    $result['status'] = "pesan";
            }
            else
                $result['status'] = "free";
            echo json_encode($result);
        }
        
        function add_kamar()
        {
            $data['kategori_kamar'] = $this->kategori_kamar_model->get_all_kategori_kamar();
            $data['kamar'] = $this->kamar_model->get_all_kamar();
            $this->load->view('header');
            $this->load->view('kamar/add_kamar',$data);
            $this->load->view('footer');
        }
        
        function add_kamar_process()
        {
            $data = $_GET;
            if(isset($_GET['adjoining']))
            {
                $adjoining = $_GET['adjoining'];
                unset($data['adjoining']);
            }
            $insert_id = $this->kamar_model->add_kamar($data);
            if(isset($_GET['adjoining']))
            {
                for($i=0;$i<count($adjoining);$i++)
                {
                $insert['id_room1'] = $insert_id;
                $insert['id_room2'] = $adjoining[$i];
                $this->kamar_model->add_adjoining($insert);
                }
            }
            redirect(KAMAR);
        }
        
        function edit_kamar($id)
        {
            $data = $this->kamar_model->get_kamar($id);
            $data['kategori_kamar'] = $this->kategori_kamar_model->get_all_kategori_kamar();
            $data['kamar'] = $this->kamar_model->get_all_kamar();
            $data['kamar_adjoining'] = $this->kamar_model->get_all_id_adjoining_by_kamar($id);
            $this->load->view('header');
            $this->load->view('kamar/form_edit_kamar',$data);
            $this->load->view('footer');
        }
        
        function edit_kamar_process()
        {
            $data = $_GET;
            if(isset($_GET['adjoining']))
            {
                $adjoining = $_GET['adjoining'];
                $this->kamar_model->delete_adjoining_by_kamar($_GET['id']);
                for($i=0;$i<count($adjoining);$i++)
                {
                $insert['id_room1'] = $_GET['id'];
                $insert['id_room2'] = $adjoining[$i];
                $this->kamar_model->add_adjoining($insert);
                }
                unset($data['adjoining']);
            }
            $this->kamar_model->update_kamar($data);
            redirect(KAMAR);
        }
        
        function delete_kamar($id)
        {
            $this->kamar_model->delete_kamar($id);
            $this->kamar_model->delete_adjoining_by_kamar($id);
            redirect(KAMAR);
        }
        
        function adjoining_room_status()
	{
            $id_kamar=$this->input->post("id_kamar");
            $tanggal_masuk=$this->input->post("tanggalmasuk");
            $tanggal_keluar=$this->input->post("tanggalkeluar");
            $select = 'kamar_id';
            $clause = "(date_pesan_masuk  <= '".$tanggal_masuk."' AND date_pesan_keluar >= '".$tanggal_masuk."')";
            $clause .= " OR (date_pesan_masuk  >= '".$tanggal_masuk."' AND date_pesan_masuk <= '".$tanggal_keluar."')";
            $result['used'] = $this->data_customer_model->get_all_customer_clause($clause,$select);
            $result['all'] = $this->kamar_model->get_all_adjoining_by_kamar($id_kamar);
            $result['adjoining'] = array();
            for($i=0;$i<count($result['all']);$i++){ 
                if($id_kamar==$result['all'][$i]['id_room1']) 
                    $temp = $result['all'][$i]['id_room2']; 
                else 
                    $temp = $result['all'][$i]['id_room1'];
                $temp2 = $result['all'][$i];
                $temp2['used'] = false;
                if(in_array(array('kamar_id'=>$temp), $result['used']))
                    $temp2['used'] = true; 
                array_push($result['adjoining'],$temp2);
            }
//            print_r($result);
            echo json_encode($result);
        }
}
/** End of file */

